package com.urea.system.service.impl;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.urea.common.utils.DateUtils;
import com.urea.common.utils.StringUtils;
import com.urea.common.utils.file.FileUtils;
import com.urea.system.domain.FileInfo;
import com.urea.system.service.IFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.urea.system.mapper.BannerMapper;
import com.urea.system.domain.Banner;
import com.urea.system.service.IBannerService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 轮播图Service业务层处理
 *
 * @author liuzd
 * @date 2023-08-01
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {
    @Autowired
    private BannerMapper bannerMapper;
    @Autowired
    private IFileInfoService fileInfoService;
    @Value("${ruoyi.profile}")
    private String filePath;
    @Value("${localhost.url}")
    private String url;

    /**
     * 查询轮播图
     *
     * @param id 轮播图主键
     * @return 轮播图
     */
    @Override
    public Banner selectBannerById(Long id) {
        return bannerMapper.selectBannerById(id);
    }

    /**
     * 查询轮播图列表
     *
     * @param banner 轮播图
     * @return 轮播图
     */
    @Override
    public List<Banner> selectBannerList(Banner banner) {
        return bannerMapper.selectBannerList(banner);
    }

    /**
     * 新增轮播图
     *
     * @param file   文件
     * @param banner 轮播图
     */
    @Override
    public void saveBanner(MultipartFile file, Banner banner) {
        FileInfo info = saveFile(file);
        if (Objects.nonNull(info)) {
            banner.setFileId(info.getId());
            banner.setUrl(url.concat("fileInfo/download/".concat(String.valueOf(info.getId()))));
        }
        saveOrUpdate(banner);
    }

    private FileInfo saveFile(MultipartFile file) {
        if (Objects.isNull(file)) {
            return null;
        }
        // 文件落到本地
        String yyyyMMdd = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String fileId = IdUtil.fastSimpleUUID();
        String originalFilename = file.getOriginalFilename();
        String fileName = fileId;
        FileInfo info = new FileInfo();
        info.setName(originalFilename);
        info.setSize(file.getSize());
        if (StringUtils.isNotBlank(originalFilename)) {
            String name = FileUtils.getName(originalFilename);
            String ext = name.substring(name.lastIndexOf("."));
            fileName = fileName.concat(".").concat(ext);
            info.setExt(ext);
        }
        String fPath = filePath + "/" + yyyyMMdd + "/" + fileName;
        info.setPath(fPath);
        File f = new File(fPath);
        fileInfoService.save(info);
        try {
            FileUtil.writeFromStream(file.getInputStream(), f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 修改轮播图
     *
     * @param banner 轮播图
     * @return 结果
     */
    @Override
    public int updateBanner(Banner banner) {
        return bannerMapper.updateBanner(banner);
    }

    /**
     * 批量删除轮播图
     *
     * @param ids 需要删除的轮播图主键
     * @return 结果
     */
    @Override
    public int deleteBannerByIds(Long[] ids) {
        return bannerMapper.deleteBannerByIds(ids);
    }

    /**
     * 删除轮播图信息
     *
     * @param id 轮播图主键
     * @return 结果
     */
    @Override
    public int deleteBannerById(Long id) {
        return bannerMapper.deleteBannerById(id);
    }
}
